package com.easy.dfcapi.service.impl;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.dfcapi.entity.ApiProductInfo;
import com.easy.dfcapi.entity.ApiStoreInfo;
import com.easy.dfcapi.entity.ApiUser;
import com.easy.dfcapi.mapper.ApiFollowMapper;
import com.easy.dfcapi.mapper.ApiStoreMapper;
import com.easy.dfcapi.mapper.ApiUserMapper;
import com.easy.dfcapi.mapper.ApiVerifyCodeMapper;
import com.easy.dfcapi.res.BuyerUserRes;
import com.easy.dfcapi.res.Response;
import com.easy.dfcapi.service.ApiFollowService;
import com.easy.dfcapi.utils.ApiSessionUser;
import com.easy.dfcapi.utils.ServletHelper;
import net.sf.cglib.beans.BeanMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by VULCANTSeries on 2017/5/10.
 */
@Service
@Transactional(readOnly = true)
public class ApiFollowServiceImpl implements ApiFollowService {

    @Autowired
    private ApiVerifyCodeMapper verifyCodeMapper;

    @Autowired
    private ApiUserMapper userMapper;

    @Autowired
    private ApiStoreMapper storeMapper;

    @Autowired
    private ApiFollowMapper followMapper;

    @Override
    public Result FollowInfo(Response<BuyerUserRes> response) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            Map<String,Object> map = new HashMap<String,Object>();//
            Integer  favoriteNum = followMapper.getFavoriteNum(sessionUser.getUserId(),2);//2-商品
            Integer followNum = followMapper.getFollowNum(sessionUser.getUserId(),1);//1-厂商
            Integer footStepNum = followMapper.getFootStepNum(sessionUser.getUserId(),2);//2-商品
            map.put("favoriteNum",favoriteNum);
            map.put("followNum",followNum);
            map.put("footStepNum",followNum);
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(map);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result FavoriteProduct(Response<BuyerUserRes> response) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            List<ApiProductInfo> favoriteList = followMapper.FavoriteProduct(sessionUser.getUserId(),data.getPageIndex(),data.getPageSize());
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(favoriteList);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result FollowProduct(Response<BuyerUserRes> response) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            List<ApiProductInfo> favoriteList = followMapper.FollowProduct(sessionUser.getUserId(),data.getPageIndex(),data.getPageSize());
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(favoriteList);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result FootStepProduct(Response<BuyerUserRes> response) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            List<ApiProductInfo> favoriteList = followMapper.FootStepProduct(sessionUser.getUserId(),data.getPageIndex(),data.getPageSize());
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(favoriteList);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }
    @Override
    public Result FavoriteStore(Response<BuyerUserRes> response) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            List<ApiStoreInfo> storeList = followMapper.FavoriteStore(sessionUser.getUserId(),data.getPageIndex(),data.getPageSize());
            List list = new ArrayList();
            for(int i =0; i < storeList.size(); i++){
                ApiStoreInfo store = storeList.get(i);
                List<Map<String,Object>> productList = storeMapper.getNiceProductByStoreId(store.getStoreId());
                Map<String,Object> map = new HashMap<String,Object>();

                BeanMap beanMap = BeanMap.create(store);
                for (Object key : beanMap.keySet()) {
                    map.put(key+"", beanMap.get(key));
                }
                map.put("productList",productList);

                list.add(map);
            }
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(list);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result FollowStore(Response<BuyerUserRes> response) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            List<ApiStoreInfo> storeList = followMapper.FollowStore(sessionUser.getUserId(),data.getPageIndex(),data.getPageSize());
            List list = new ArrayList();
            for(int i =0; i < storeList.size(); i++){
                ApiStoreInfo store = storeList.get(i);
                List<Map<String,Object>> productList = storeMapper.getNiceProductByStoreId(store.getStoreId());
                Map<String,Object> map = new HashMap<String,Object>();

                BeanMap beanMap = BeanMap.create(store);
                for (Object key : beanMap.keySet()) {
                    map.put(key+"", beanMap.get(key));
                }
                map.put("productList",productList);

                list.add(map);
            }
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(list);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result FootStepStore(Response<BuyerUserRes> response) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            List<ApiStoreInfo> storeList = followMapper.FootStepStore(sessionUser.getUserId(),data.getPageIndex(),data.getPageSize());
            List list = new ArrayList();
            for(int i =0; i < storeList.size(); i++){
                ApiStoreInfo store = storeList.get(i);
                List<Map<String,Object>> productList = storeMapper.getNiceProductByStoreId(store.getStoreId());
                Map<String,Object> map = new HashMap<String,Object>();

                BeanMap beanMap = BeanMap.create(store);
                for (Object key : beanMap.keySet()) {
                    map.put(key+"", beanMap.get(key));
                }
                map.put("productList",productList);

                list.add(map);
            }
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(list);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result AddFavorite(Response<BuyerUserRes> response, Integer type) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            followMapper.DelFavorite(sessionUser.getUserId(), data.getObjectId(), type);
            followMapper.AddFavorite(sessionUser.getUserId(), data.getObjectId(), type);
            rt.setStatus(EnumConfig.S_CODE.OK);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result AddFollow(Response<BuyerUserRes> response, Integer type) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            followMapper.DelFollow(sessionUser.getUserId(), data.getObjectId(), type);
            followMapper.AddFollow(sessionUser.getUserId(), data.getObjectId(), type);
            rt.setStatus(EnumConfig.S_CODE.OK);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result AddFootStep(Response<BuyerUserRes> response, Integer type) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            followMapper.DelFootStep(sessionUser.getUserId(), data.getObjectId(), type);
            followMapper.AddFootStep(sessionUser.getUserId(), data.getObjectId(), type);
            rt.setStatus(EnumConfig.S_CODE.OK);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result DelFavorite(Response<BuyerUserRes> response, Integer type) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            followMapper.DelFavorite(sessionUser.getUserId(), data.getObjectId(), type);
            rt.setStatus(EnumConfig.S_CODE.OK);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }
        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result DelFollow(Response<BuyerUserRes> response, Integer type) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            followMapper.DelFollow(sessionUser.getUserId(), data.getObjectId(), type);
            rt.setStatus(EnumConfig.S_CODE.OK);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    @Transactional(readOnly = false)
    public Result DelFootStep(Response<BuyerUserRes> response, Integer type) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            followMapper.DelFootStep(sessionUser.getUserId(), data.getObjectId(), type);
            rt.setStatus(EnumConfig.S_CODE.OK);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result FavoriteProductOld(Response<BuyerUserRes> response) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            List<ApiProductInfo> favoriteList = followMapper.FavoriteProductOld(sessionUser.getUserId(),data.getPageIndex(),data.getPageSize());
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(favoriteList);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result FollowStoreOld(Response<BuyerUserRes> response) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            List<ApiStoreInfo> storeList = followMapper.FollowStoreOld(sessionUser.getUserId(),data.getPageIndex(),data.getPageSize());
            List list = new ArrayList();
            for(int i =0; i < storeList.size(); i++){
                ApiStoreInfo store = storeList.get(i);
                List<Map<String,Object>> productList = storeMapper.getNiceProductByStoreId(store.getStoreId());
                Map<String,Object> map = new HashMap<String,Object>();

                BeanMap beanMap = BeanMap.create(store);
                for (Object key : beanMap.keySet()) {
                    map.put(key+"", beanMap.get(key));
                }
                map.put("productList",productList);

                list.add(map);
            }
            rt.setStatus(EnumConfig.S_CODE.OK);
            rt.setData(list);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result HaveFavorite(Response<BuyerUserRes> response, int type) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            if(followMapper.HaveFavorite(sessionUser.getUserId(), data.getObjectId(), type)>0)rt.setData(true);
            else rt.setData(false);
            rt.setStatus(EnumConfig.S_CODE.OK);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }

    @Override
    public Result HaveFollow(Response<BuyerUserRes> response, Integer type) throws Exception{
        Result rt = new Result();
        try{
            BuyerUserRes data = response.getData(BuyerUserRes.class);
            ApiSessionUser sessionUser = ServletHelper.getSessionUserByToken(response.getToken());
            if(sessionUser == null){
                rt.setStatus(EnumConfig.S_CODE.NOT_USER);
                return rt;
            }

            ApiUser user = userMapper.getByUserId(sessionUser.getUserId());
            if(user==null){
                rt.setStatus(EnumConfig.S_CODE.VALID_FAILED);
                return rt;
            }

            if(followMapper.HaveFollow(sessionUser.getUserId(), data.getObjectId(), type)>0)rt.setData(true);
            else rt.setData(false);
            rt.setStatus(EnumConfig.S_CODE.OK);
        }catch(Exception ex){
            rt.setStatus(EnumConfig.S_CODE.FAIL);
            throw ex;
        }

        return rt;
    }


}
